import java.util.TreeSet;

/*
    TreeSet无法对自定义类型排序，即没有实现java.lang.Comparable接口，没有实现compareTo方法
    想要自定义排序就得实现java.lang.Comparable接口，实现compareTo方法
 */
public class TreeSetTest01 {
    public static void main(String[] args) {
        TreeSet<Vip> vips = new TreeSet<>();
        vips.add(new Vip("zhangsi",20));
        vips.add(new Vip("zhangsan",20));
        vips.add(new Vip("king",22));
        vips.add(new Vip("soft",22));
        vips.add(new Vip("文梦甜",22));
        vips.add(new Vip("王强",22));
        vips.add(new Vip("王小绕",0));
        vips.add(new Vip("文大甜",2));


        for (Vip vip: vips) {        //自动排序
            System.out.println(vip);
        }

    }
}

class Vip implements Comparable<Vip>{
    String name;
    int age;

    public Vip(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Vip{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    /*
    compareTo方法的返回值很重要：
        返回0表示相同，value会覆盖
        返回>0，会继续在右子树上找
        返回<0，会继续在左子树上找
    */
    @Override
    public int compareTo(Vip vip) {
        //排序规则
        if(this.age == vip.age){
            return this.name.compareTo(vip.name);
        }else{
            return this.age - vip.age;
        }

    }
}